toast = new Object();

toast.info = function(msg, func, time) {
	var toast = $("#toast-info");
	toast.find("[data-dialog-data='msg']").text(msg);
	window.info = func;
	toast.modal();
	if (time == undefined) {
		time = 3000;
	}
	if (time != -1) {
		setTimeout(()=>toast.modal('hide'), time);
	}
	return true;
};

toast.warning = function(msg, func, time) {
	var toast = $("#toast-warning");
	toast.find("[data-dialog-data='msg']").text(msg);
	window.warning = func;
	toast.modal();
	if (time == undefined) {
		time = 4000;
	}
	if (time != -1) {
		setTimeout(()=>toast.modal('hide'), time);
	}
	return true;
};

toast.success = function(msg, func, time) {
	var toast = $("#toast-success");
	toast.find("[data-dialog-data='msg']").text(msg);
	window.success = func;
	toast.modal();
	if (time == undefined) {
		time = 2000;
	}
	if (time != -1) {
		setTimeout(()=>toast.modal('hide'), time);
	}
	return true;
};

toast.failure = function(msg, func, time) {
	var toast = $("#toast-failure");
	toast.find("[data-dialog-data='msg']").text(msg);
	window.failure = func;
	toast.modal();
	if (time == undefined) {
		time = 5000;
	}
	if (time != -1) {
		setTimeout(()=>toast.modal('hide'), time);
	}
	return true;
};

toast.justInfo = function(msg, func) {
	var toast = $("#toast-info");
	toast.find("[data-dialog-data='msg']").text(msg);
	window.info = func;
	toast.modal();
};
toast.justWarning = function(msg, func) {
	var toast = $("#toast-warning");
	toast.find("[data-dialog-data='msg']").text(msg);
	window.warning = func;
	toast.modal();
};
toast.justSuccess = function(msg, func) {
	var toast = $("#toast-success");
	toast.find("[data-dialog-data='msg']").text(msg);
	window.success = func;
	toast.modal();
};
toast.justFailure = function(msg, func) {
	var toast = $("#toast-failure");
	toast.find("[data-dialog-data='msg']").text(msg);
	window.failure = func;
	toast.modal();
};

toast.infoThis = function(obj) {
	let $obj = $(obj);
	let msg = $obj.attr("data-toast-msg");
	let func = $obj.attr("data-toast-func");
	let time = parseInt($obj.attr("data-toast-msg"));
	toast.info(msg, func, time);
};
toast.warningThis = function(obj) {
	let $obj = $(obj);
	let msg = $obj.attr("data-toast-msg");
	let func = $obj.attr("data-toast-func");
	let time = parseInt($obj.attr("data-toast-msg"));
	toast.warning(msg, func, time);
};
toast.successThis = function(obj) {
	let $obj = $(obj);
	let msg = $obj.attr("data-toast-msg");
	let func = $obj.attr("data-toast-func");
	let time = parseInt($obj.attr("data-toast-msg"));
	toast.success(msg, func, time);
};
toast.failureThis = function(obj) {
	let $obj = $(obj);
	let msg = $obj.attr("data-toast-msg");
	let func = $obj.attr("data-toast-func");
	let time = parseInt($obj.attr("data-toast-msg"));
	toast.failure(msg, func, time);
};

toast.loading = function(msg) {
	var toast = $("#toast-loading");
	toast.find("[data-dialog-data='msg']").text(msg);
	// SHOW DIALOG
	toast.modal();
};

toast.close = function(func) {
	var toast = $("#toast-loading");
	window.loading = func;
	toast.modal('hide');
};

//=========
$(function () {
	var html = "<div class=\"modal fade\"id=\"toast-loading\"data-backdrop=\"static\"><div class=\"modal-dialog alert alert-info\"style=\"width:200px\"style=\"top:30%\"><h4><i class=\"fa fa-refresh fa-spin\"></i>等待</h4><p data-dialog-data=\"msg\"></p></div></div><div class=\"modal fade\"id=\"toast-info\"><div class=\"modal-dialog alert alert-info alert-dismissible\"style=\"top:30%\"><button type=\"button\"class=\"close\"data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button><h4><i class=\"icon fa fa-info\"></i>提示</h4><p data-dialog-data=\"msg\"></p></div></div><div class=\"modal fade\"id=\"toast-warning\"><div class=\"modal-dialog alert alert-warning alert-dismissible\"style=\"top:30%\"><button type=\"button\"class=\"close\"data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button><h4><i class=\"icon fa fa-warning\"></i>警告</h4><p data-dialog-data=\"msg\"></p></div></div><div class=\"modal fade\"id=\"toast-success\"><div class=\"modal-dialog alert alert-success alert-dismissible\"style=\"top:30%\"><button type=\"button\"class=\"close\"data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button><h4><i class=\"icon fa fa-check\"></i>成功</h4><p data-dialog-data=\"msg\"></p></div></div><div class=\"modal fade\"id=\"toast-failure\"><div class=\"modal-dialog alert alert-danger alert-dismissible\"style=\"top:30%\"><button type=\"button\"class=\"close\"data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button><h4><i class=\"icon fa fa-times\"></i>失败</h4><p data-dialog-data=\"msg\"></p></div></div>";
	$("body").children(":first").before(html);
	
	var loading = $("#toast-loading");
	loading.on('hide.bs.modal', function () {
		if (window.loading != undefined) {
			window.loading();
		}
	});
	
	var info = $("#toast-info");
	info.on('hide.bs.modal', function () {
		if (window.info != undefined) {
			window.info();
		}
	});
	
	var warning = $("#toast-warning");
	warning.on('hide.bs.modal', function () {
		if (window.warning != undefined) {
			window.warning();
		}
	});
	
	var success = $("#toast-success");
	success.on('hide.bs.modal', function () {
		if (window.success != undefined) {
			window.success();
		}
	});
	
	var failure = $("#toast-failure");
	failure.on('hide.bs.modal', function () {
		if (window.failure != undefined) {
			window.failure();
		}
	});
	
});

